<template>
  <el-dialog title="提交申请" :visible.sync="visible" width="600px" destroy-on-close @close="closeDialog">
    <el-form ref="formData" v-loading="loading" :rules="rules" :model="formData" status-icon>
      <el-form-item label="审批意见" prop="message" label-width="120px">
        <el-input
          v-model="formData.message"
          type="textarea"
          maxlength="300"
          placeholder="请输入审批意见"
          :autosize="{ minRows: 4}"
          show-word-limit
        />
      </el-form-item>
      <el-form-item v-if="nextNodes && nextNodes.length>0" label="下一步审批人" prop="assigneeMap" label-width="120px">
        <div v-for="(item, index) in nextNodes" :key="index">
          <span>【{{ item.name }}】：</span>
          <el-input
            v-model="formData.assigneeMap[item.id]"
            :placeholder="`请输入[${item.name}]用户名`"
            maxlength="100"
            show-word-limit
            type="text"
            style="margin-bottom: 10px"
          />
        </div>
        <div>可输入用户名：【李总经理,郑主管,王秘书】</div>
        <div>可同时指定多个用户（用英文逗号分隔）</div>
      </el-form-item>
      <el-form-item align="center">
        <el-button type="primary" size="mini" @click="submitForm('formData')">提交</el-button>
        <el-button size="mini" @click="visible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import api from '@/api/workflow/task'

export default {
  props: {
    taskId: String
  },

  data() {
    return {
      visible: false, // 弹出窗口，true弹出
      loading: false,
      formData: { // 提交表单数据
        message: null,
        assigneeMap: {}
      },
      nextNodes: [], // 下一节点是否为结束节点
      rules: {
        assigneeMap: [
          { required: true, message: '请输入下一步审批人', trigger: 'blur' }
        ]
      }
    }
  },

  watch: {
    async visible(newVal) {
      if (newVal) {
        try {
          this.loading = true
          // 获取下一节点信息
          const { data } = await api.getNextNodeInfo(this.taskId)
          this.nextNodes = data
          this.loading = false
        } catch (error) {
          this.loading = false
        }
      }
    }
  },

  methods: {
    // 提交表单数据
    submitForm(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          // 校验通过，提交表单数据
          this.loading = true
          try {
            // 审批人转为数组
            const data = {
              taskId: this.taskId,
              message: this.formData.message,
              assigneeMap: this.formData.assigneeMap // 审批人转为数组
            }
            const response = await api.completeTask(data)
            if (response.code === 200) {
              // 刷新数据
              this.$parent.fetchData()
              this.$message.success('提交成功')
              // 将表单清空
              this.$refs[formName].resetFields()
              // 关闭窗口
              this.visible = false
            }
            this.loading = false
          } catch (e) {
            this.loading = false
          }
        }
      })
    },

    // 关闭
    closeDialog() {
      // 将表单清空
      this.$refs['formData'].resetFields()
      this.formData = { message: null, assigneeMap: {}}
      this.visible = false
    }

  }
}
</script>
